//
//  YDViewController.m
//  CRM
//
//  Created by YD_iOS on 16/7/21.
//  Copyright © 2016年 YD_iOS. All rights reserved.
//

#import "YDViewController.h"
#import "ViewController.h"

#define isIOS9 ([[UIDevice currentDevice].systemVersion intValue]>=9?YES:NO)
#define isIOS10 ([[UIDevice currentDevice].systemVersion intValue]>=10?YES:NO)

@interface YDViewController ()


//记住tableview原来的偏移量，编辑完成后改回来
@property (nonatomic, assign) CGPoint oldOffset;

@property (nonatomic, strong) UITableViewCell *tempCell;



@end

@implementation YDViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = kViewControllerBackgroundColor;
    
    //添加的蓝色不透明背景，有时候会挡住title
    UIView *navBGView = [self.navigationController.navigationBar viewWithTag:30000];
    [self.navigationController.navigationBar sendSubviewToBack:navBGView];
    
    //防止tableview 或 scrollview 自动下移64
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;

}

- (void)viewWillAppear:(BOOL)animated
{
    [self changeNavigationBarBackgroundColor:kNavigationBackgroundColor];
    [super viewWillAppear:animated];
}

- (void)isNeedAgainLoginWithErrorCode:(NSString *)errorCode
{
    if ([errorCode isEqualToString:@"LOGIN_SID_FAL_0X001"] || [errorCode isEqualToString:@"LOGIN_FAL_0x002"] || [errorCode isEqualToString:@"NO_MATH_CONDITION"]) {
        //删除数据
        [kUserDefaults removeObjectForKey:kAPP_SID];
    
        UIStoryboard *main = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        ViewController *vc = [main instantiateViewControllerWithIdentifier:@"ViewController"];
        [self presentViewController:vc animated:YES completion:^{
            self.view.window.rootViewController = vc;
            [MBProgressHUD showTips:@"登录已失效，请重新登陆" toView:kWindow];
        }];
    }
}



#pragma mark - 动态修改状态栏跟顶部导航栏的颜色
-(void)changeNavigationBarBackgroundColor:(UIColor *)barBackgroundColor{
    if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]){
        NSArray *subviews =self.navigationController.navigationBar.subviews;
        for (id viewObj in subviews) {
            if (isIOS10) {
                //iOS10,改变了状态栏的类为_UIBarBackground
                NSString *classStr = [NSString stringWithUTF8String:object_getClassName(viewObj)];
                if ([classStr isEqualToString:@"_UIBarBackground"]) {
                    UIImageView *imageView=(UIImageView *)viewObj;
                    imageView.hidden=YES;
                }
            }else{
                //iOS9以及iOS9之前使用的是_UINavigationBarBackground
                NSString *classStr = [NSString stringWithUTF8String:object_getClassName(viewObj)];
                if ([classStr isEqualToString:@"_UINavigationBarBackground"]) {
                    UIImageView *imageView=(UIImageView *)viewObj;
                    imageView.hidden=YES;
                }
            }
        }
        UIImageView *imageView = [self.navigationController.navigationBar viewWithTag:111];
        if (!imageView) {
            imageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, -20, self.view.width, 64)];
            imageView.tag = 111;
            [imageView setBackgroundColor:barBackgroundColor];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.navigationController.navigationBar insertSubview:imageView atIndex:0];
            });
        }else{
            [imageView setBackgroundColor:barBackgroundColor];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.navigationController.navigationBar sendSubviewToBack:imageView];
            });
            
        }
        
    }
}

@end
